// SyncSetting.cpp : Implementation of CSyncSetting

#include "stdafx.h"
#include "SyncSetting.h"
#include "../commonlib/Log.h"


// CSyncSetting
void CSyncSetting::LoadNativeForm()
{
	//find service provider
	if(!regesiter)
		regesiter = new Regesiter();
	std::wstring serviceProvider;
	std::wstring serviceUri;
	std::wstring userEmail;
	std::wstring reminderPhone;
	bool EnableContact;
	bool EnableReminder;

	HWND editUrl;
	HWND editEmail;
	HWND chkContact;
	HWND chkReminder;
	HWND editMobile;	
	//disable the edit firstly
	editMobile = ::GetDlgItem(m_hWnd,IDC_EDIT_MOBILE);
	::EnableWindow(editMobile,FALSE);

	if(regesiter->LoadSyncServiceInfo(serviceProvider,serviceUri,userEmail,reminderPhone,EnableContact,EnableReminder) != 0) return;

	//uri
	editUrl = ::GetDlgItem(m_hWnd,IDC_EDIT_URL);
	::SetWindowTextW(editUrl,serviceUri.c_str());

	//email
	editEmail = ::GetDlgItem(m_hWnd,IDC_EDIT_EMAIL);
	::SetWindowTextW(editEmail,userEmail.c_str());

	//contact check
	chkContact = ::GetDlgItem(m_hWnd,IDC_CHECK_CONTACT);
	if(EnableContact)
		::SendMessageW(chkContact,BM_SETCHECK,(WPARAM)BST_CHECKED,0);
	else
		::SendMessageW(chkContact,BM_SETCHECK,(WPARAM)BST_UNCHECKED,0);
	//reminder check and mobile edit
	chkReminder = ::GetDlgItem(m_hWnd,IDC_CHECK_REMINDER);
	if(EnableReminder){
		::SendMessageW(chkReminder,BM_SETCHECK,(WPARAM)BST_CHECKED,0);
		::EnableWindow(editMobile,TRUE);
	}
	else{
		::SendMessageW(chkReminder,BM_SETCHECK,(WPARAM)BST_UNCHECKED,0);
		::EnableWindow(editMobile,FALSE);
	}
	//mobile edit text
	::SetWindowTextW(editMobile,reminderPhone.c_str());
}


LRESULT CSyncSetting::OnClickedOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	//find service provider
	if(!regesiter)
		regesiter = new Regesiter();
	std::wstring serviceProvider;
	std::wstring serviceUri;
	std::wstring userEmail;
	std::wstring reminderPhone;
	bool EnableContact = false;
	bool EnableReminder = false;

	int nLengthUrl;
	int nLengthEmail;
	int nLengthMobile;

	HWND editUrl;
	HWND editEmail;
	HWND chkContact;
	HWND chkReminder;
	HWND editMobile;
	//uri
	editUrl = ::GetDlgItem(m_hWnd,IDC_EDIT_URL);
	nLengthUrl = ::GetWindowTextLengthW(editUrl);
	if(nLengthUrl == 0){
		::MessageBoxW(0,L"The serivce Url cannot be empty!",L"Tips",MB_ICONWARNING);
		return 0;
	}
	//email
	editEmail = ::GetDlgItem(m_hWnd,IDC_EDIT_EMAIL);
	nLengthEmail = ::GetWindowTextLengthW(editEmail);
	if(nLengthEmail == 0){
		::MessageBoxW(0,L"The use email cannot be empty!",L"Tips",MB_ICONWARNING);
		return 0;
	}


	serviceUri.clear();
	userEmail.clear();
	serviceUri.resize(nLengthUrl+1);
	userEmail.resize(nLengthEmail + 1);
	::GetWindowTextW(editUrl,(LPWSTR)serviceUri.data(),nLengthUrl+1);
	::GetWindowTextW(editEmail,(LPWSTR)userEmail.data(),nLengthEmail+1);


	//contact check
	chkContact = ::GetDlgItem(m_hWnd,IDC_CHECK_CONTACT);
	if(::SendMessageW(chkContact,BM_GETCHECK,0,0) == BST_CHECKED)
		EnableContact = true;

	//reminder check
	chkReminder = ::GetDlgItem(m_hWnd,IDC_CHECK_REMINDER);
	if(::SendMessageW(chkReminder,BM_GETCHECK,0,0) == BST_CHECKED)
		EnableReminder = true;

	//mobile edit text
	editMobile = ::GetDlgItem(m_hWnd,IDC_EDIT_MOBILE);
	nLengthMobile = ::GetWindowTextLengthW(editMobile);
	if(nLengthMobile > 0){
		reminderPhone.clear();
		reminderPhone.resize(nLengthMobile+1);
		::GetWindowTextW(editMobile,(LPWSTR)reminderPhone.data(),nLengthMobile+1);
	}
	else if(EnableReminder){
		::MessageBoxW(0,L"You must set a mobile for the reminder!",L"Tips",MB_ICONWARNING);
		return 0;
	}

	if(regesiter->SyncServiceInfoRegesiter(serviceProvider,serviceUri,userEmail,reminderPhone,EnableContact,EnableReminder) < 0){
		::MessageBoxW(0,L"Failed to save",L"Error",MB_ICONWARNING);
		return 0;
	}

	EndDialog(wID);
	return 0;
}

LRESULT CSyncSetting::OnBnClickedCheckReminder(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	//reminder check and mobile edit
	HWND chkReminder = ::GetDlgItem(m_hWnd,IDC_CHECK_REMINDER);
	HWND editMobile =  ::GetDlgItem(m_hWnd,IDC_EDIT_MOBILE);
	if(::SendMessageW(chkReminder,BM_GETCHECK,0,0) == BST_CHECKED)
		::EnableWindow(editMobile,TRUE);
	else
		::EnableWindow(editMobile,FALSE);
	return 0;
}
